﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
//using DevExpress.XtraPrintingLinks;
using Lists;
using System.Linq;

namespace BasicForms
{
    public partial class FrmSupportPrintComponents : FrmBase
    {
        public DBDataContext DB = new DBDataContext(GeneralLibs.LocalSettings.ConnectString);
        public FrmSupportPrintComponents()
        {
            InitializeComponent();
            bandedGridView1.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(bandedGridView1_RowStyle);
        }
        bool _PrintTenCongTy;
        bool _PrintDiaChiDienThoai;
        string _TheoMauDong1 = "";
        string _TheoMauDong2 = "";
        string _Title = ""; // Tiêu đề BC
        string _More1 = ""; // sub Title
        string _More2 = ""; // Góc dưới bên phải báo cáo header
        string _More3 = ""; // Góc dưới bên trái báo cáo header
        string _FooterTopLeft = ""; // Góc trên bên trái của footer ngay dưới bảng dữ liệu
        string _FooterDienGiai = "";
        string _FooterNgay = "";
        string _Footer1 = "";
        string _Footer2 = "";
        string _Footer3 = "";
        string tenCongTy = "";
        string diaChiCongTy = "";
        protected void PrintSimple(bool useLanscapeView, System.Drawing.Printing.PaperKind paperKind, IPrintable printableComponent)
        {
            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = paperKind;
            printableComponentLink.Landscape = useLanscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.PageHeaderFooter = new DevExpress.XtraPrinting.PageHeaderFooter(null, new DevExpress.XtraPrinting.PageFooterArea(new string[] {
                "",
                "Trang [Page #]",
                ""}, new System.Drawing.Font("Times New Roman", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), DevExpress.XtraPrinting.BrickAlignment.Near));
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }
        protected void PrintSimpleScale(bool useLanscapeView, System.Drawing.Printing.PaperKind paperKind, IPrintable printableComponent, int pageToScale, float headerFooterFontSize)
        {
            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = paperKind;
            printableComponentLink.Landscape = useLanscapeView;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.Margins.Bottom = 40;
            if (headerFooterFontSize > 0)
            {
                printableComponentLink.PageHeaderFooter = new DevExpress.XtraPrinting.PageHeaderFooter(null, new DevExpress.XtraPrinting.PageFooterArea(new string[] {
                "",
                "Trang [Page #]",
                ""}, new System.Drawing.Font("Times New Roman", headerFooterFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), DevExpress.XtraPrinting.BrickAlignment.Near));
            }
            else
            {
                printableComponentLink.PageHeaderFooter = new DevExpress.XtraPrinting.PageHeaderFooter(null, new DevExpress.XtraPrinting.PageFooterArea(new string[] {
                "",
                "Trang [Page #]",
                ""}, new System.Drawing.Font("Times New Roman", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), DevExpress.XtraPrinting.BrickAlignment.Near));
            }
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
            if (pageToScale > 0)
            {
                printableComponentLink.PrintingSystem.Document.AutoFitToPagesWidth = pageToScale;
                //printableComponentLink.PrintingSystem.Document.ScaleFactor = 1;
            }
        }
        
        protected void PrintWithHeader(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai, string title, string more1)
        {
            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = more1;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }
        protected void PrintWithHeader(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai, string title, string more1, string more2)
        {
            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = more1;
            _More2 = more2;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }

        public void PrintWithHeaderAndFooter(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
           string title, string header1, string header2,
           string footer1, string footer2)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _Footer1 = footer1;
            _Footer2 = footer2;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea2Columns);            
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }

        public void PrintWithHeaderAndFooter(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
               string title, string header1, string header2,
               string footer1, string footer2, string footer3)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _Footer1 = footer1;
            _Footer2 = footer2;
            _Footer3 = footer3;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea3Columns);            
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }

        public void PrintWithHeaderAndFooter(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
              string title, string header1, string header2,
              string footerNgay, string footer1, string footer2, string footer3)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _FooterNgay = footerNgay;
            _Footer1 = footer1;
            _Footer2 = footer2;
            _Footer3 = footer3;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea3Columns);            
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }

        public void PrintWithHeaderAndFooter(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
         string theoMauBaoCaoDong1, string theoMauBaoCaoDong2, string title, string header1, string header2,
         string footerDienGiai, string footerNgay, string footer1, string footer2, string footer3)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;            

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _TheoMauDong1 = theoMauBaoCaoDong1;
            _TheoMauDong2 = theoMauBaoCaoDong2;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _FooterNgay = footerNgay;
            _Footer1 = footer1;
            _Footer2 = footer2;
            _Footer3 = footer3;
            _FooterDienGiai = footerDienGiai;
                        
            printableComponentLink.Component = printableComponent;            
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea3Columns);                     
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();            
            
        }

        public void PrintWithHeaderAndFooter(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
         string theoMauBaoCaoDong1, string theoMauBaoCaoDong2, string title, string header1, string header2, string header3,
         string footerTopLeft, string footerDienGiai, string footerNgay, string footer1, string footer2, string footer3)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _TheoMauDong1 = theoMauBaoCaoDong1;
            _TheoMauDong2 = theoMauBaoCaoDong2;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _More3 = header3;
            _FooterNgay = footerNgay;
            _Footer1 = footer1;
            _Footer2 = footer2;
            _Footer3 = footer3;
            _FooterTopLeft = footerTopLeft;
            _FooterDienGiai = footerDienGiai;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea3Columns);
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();

        }

        public void PrintWithHeaderAndFooter(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
         string theoMauBaoCaoDong1, string theoMauBaoCaoDong2, string title, string header1, string header2,
         string footerDienGiai, string footerNgay, string footer1, string footer2)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _TheoMauDong1 = theoMauBaoCaoDong1;
            _TheoMauDong2 = theoMauBaoCaoDong2;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _FooterNgay = footerNgay;
            _Footer1 = footer1;
            _Footer2 = footer2;
            _FooterDienGiai = footerDienGiai;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;            
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea2Columns);            
            printableComponentLink.CreateDocument();                        
            printableComponentLink.ShowPreview();            
        }                    

        public void PrintWithHeaderAndFooter(System.Drawing.Printing.PaperKind pageKind, bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
         string theoMauBaoCaoDong1, string theoMauBaoCaoDong2, string title, string header1, string header2,
         string footerDienGiai, string footerNgay, string footer1, string footer2)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _TheoMauDong1 = theoMauBaoCaoDong1;
            _TheoMauDong2 = theoMauBaoCaoDong2;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _FooterNgay = footerNgay;
            _Footer1 = footer1;
            _Footer2 = footer2;
            _FooterDienGiai = footerDienGiai;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = pageKind;            
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.Margins.Bottom = 40;
            printableComponentLink.Margins.Left = 40;
            printableComponentLink.Margins.Right = 40;
            printableComponentLink.Margins.Top = 40;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea2Columns);            
            printableComponentLink.CreateDocument();            
            printableComponentLink.ShowPreview();               
        }

        private void printableComponentLink_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
        {
            BrickGraphics gr = printableComponentLink.PrintingSystem.Graph;
            gr.Modifier = BrickModifier.ReportHeader;
            int currentY = 0;
            if (_PrintTenCongTy)
            {
                gr.StringFormat = new BrickStringFormat(StringAlignment.Near);
                gr.Font = new Font("Times New Roman", 12, FontStyle.Bold);
                RectangleF rectTapDoan = new RectangleF(0, currentY, gr.ClientPageSize.Width, 20);
                currentY += 20;
                gr.DrawString(tenCongTy, System.Drawing.Color.Black, rectTapDoan, BorderSide.None);

                
                gr.StringFormat = new BrickStringFormat(StringAlignment.Near);
                gr.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rectCongTy = new RectangleF(0, currentY, gr.ClientPageSize.Width, 20);
                currentY += 20;
                gr.DrawString(diaChiCongTy, System.Drawing.Color.Black, rectCongTy, BorderSide.None);
            }
            if (_TheoMauDong1.Length > 0)
            {
                gr.StringFormat = new BrickStringFormat(StringAlignment.Center);
                gr.Font = new Font("Times New Roman", 12, FontStyle.Bold);
                RectangleF rectDong1 = new RectangleF(gr.ClientPageSize.Width - 300, 0, 300, 30);
                gr.DrawString(_TheoMauDong1, System.Drawing.Color.Black, rectDong1, BorderSide.None);
                currentY += 20;
            }
            if (_TheoMauDong2.Length > 0)
            {
                gr.StringFormat = new BrickStringFormat(StringAlignment.Center);
                gr.Font = new Font("Times New Roman", 12, FontStyle.Italic);
                RectangleF rectDong2 = new RectangleF(gr.ClientPageSize.Width - 300, 20, 300, 50);
                gr.DrawString(_TheoMauDong2, System.Drawing.Color.Black, rectDong2, BorderSide.None);
                currentY += 10;
            }
            if (_Title.Length > 0)
            {
                int _Height = 0;
                string[] _AMore1 = _Title.Split('\n');
                foreach (string _StringSplit in _AMore1)
                {
                    _Height += 30;
                }
                gr.StringFormat = new BrickStringFormat(StringAlignment.Center);
                gr.Font = new Font("Times New Roman", 16, FontStyle.Bold);
                RectangleF rec = new RectangleF(0, currentY, gr.ClientPageSize.Width, _Height);
                currentY += _Height;
                gr.DrawString(_Title, System.Drawing.Color.Black, rec, BorderSide.None);
            }
            if (_More1.Length > 0)
            {
                int _Height = 0;
                string[] _AMore1 = _More1.Split('\n');
                foreach (string _StringSplit in _AMore1)
                {
                    _Height += 13;
                }
                _Height += 20;
                gr.StringFormat = new BrickStringFormat(StringAlignment.Center);
                gr.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rect = new RectangleF(0, currentY, gr.ClientPageSize.Width, _Height);
                gr.DrawString(_More1, System.Drawing.Color.Black, rect, BorderSide.None);
                currentY += _Height;
            }
            int more2Height = 0;
            if (_More2.Length > 0)
            {
                int _Height = 0;
                string[] _AMore1 = _More2.Split('\n');
                foreach (string _StringSplit in _AMore1)
                {
                    _Height += 20;
                }
                gr.StringFormat = new BrickStringFormat(StringAlignment.Near);
                gr.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rect = new RectangleF(0, currentY, gr.ClientPageSize.Width/3*2, _Height);
                gr.DrawString(_More2, System.Drawing.Color.Black, rect, BorderSide.None);
                if(_More3.Length <= 0) currentY += _Height;
                more2Height = _Height;
            }
            if (_More3.Length > 0)
            {
                int _Height = 0;
                string[] _AMore1 = _More3.Split('\n');
                foreach (string _StringSplit in _AMore1)
                {
                    _Height += 20;
                }
                gr.StringFormat = new BrickStringFormat(StringAlignment.Far);
                gr.Font = new Font("Times New Roman", 12, FontStyle.Italic);
                RectangleF rect = new RectangleF(e.Graph.ClientPageSize.Width - 250, currentY, gr.ClientPageSize.Width/3, _Height);
                gr.DrawString(_More3, System.Drawing.Color.Black, rect, BorderSide.None);
                currentY = more2Height > _Height ? more2Height : _Height;
            }
        }       

        private void printableComponentLink_CreateReportFooterArea2Columns(object sender, CreateAreaEventArgs e)
        {

            try
            {
                e.Graph.BackColor = System.Drawing.Color.White;
                if (_FooterNgay.Length > 0)
                {
                    e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                    e.Graph.Font = new Font("Times New Roman", 11,FontStyle.Bold |FontStyle.Italic);
                    RectangleF rectNgay = new RectangleF(e.Graph.ClientPageSize.Width - 250, 30, 250, 120);
                    e.Graph.DrawString(_FooterNgay, System.Drawing.Color.Black, rectNgay, BorderSide.None);
                }            
                if (_Footer1.Length > 0)
                {
                    e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                    e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                    RectangleF rect = new RectangleF(0, 50, 250, 120);
                    e.Graph.DrawString(_Footer1, System.Drawing.Color.Black, rect, BorderSide.None);
                }
                if (_Footer2.Length > 0)
                {
                    e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                    e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                    RectangleF rect = new RectangleF(e.Graph.ClientPageSize.Width - 250, 50, 250, 120);
                    e.Graph.DrawString(_Footer2, System.Drawing.Color.Black, rect, BorderSide.None);
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void printableComponentLink_CreateReportFooterArea3Columns(object sender, CreateAreaEventArgs e)
        {
            try
            {
                int currentY = 10;
                e.Graph.BackColor = System.Drawing.Color.White;

                if (_FooterTopLeft.Length > 0)
                {
                    e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
                    e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Regular);
                    RectangleF rectDienGiai = new RectangleF(0, currentY, e.Graph.ClientPageSize.Width, 120);
                    e.Graph.DrawString(_FooterTopLeft, System.Drawing.Color.Black, rectDienGiai, BorderSide.None);
                    string[] arS = _FooterTopLeft.Split('\n');
                    currentY += arS.Length * 20;
                }
                if (_FooterDienGiai.Length > 0)
                {
                    e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                    e.Graph.Font = new Font("Times New Roman", 11,FontStyle.Bold|FontStyle.Italic);
                    RectangleF rectDienGiai = new RectangleF(0, currentY, e.Graph.ClientPageSize.Width, 120);
                    e.Graph.DrawString(_FooterDienGiai, System.Drawing.Color.Black, rectDienGiai, BorderSide.None);
                    string[] arS = _FooterDienGiai.Split('\n');
                    currentY += arS.Length * 20;
                }
                if (_FooterNgay.Length > 0)
                {
                    e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                    e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold | FontStyle.Italic);
                    RectangleF rectNgay = new RectangleF(e.Graph.ClientPageSize.Width - 250, currentY,250, 120);
                    e.Graph.DrawString(_FooterNgay, System.Drawing.Color.Black, rectNgay, BorderSide.None);
                    currentY += 20;

                    if (_Footer1.Length > 0)
                    {
                        e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                        e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                        RectangleF rect = new RectangleF(0, currentY, 250, 120);
                        e.Graph.DrawString(_Footer1, System.Drawing.Color.Black, rect, BorderSide.None);
                    }
                    if (_Footer2.Length > 0)
                    {
                        e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                        e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                        RectangleF rect = new RectangleF((e.Graph.ClientPageSize.Width / 2) - 125, currentY, 250, 120);
                        e.Graph.DrawString(_Footer2, System.Drawing.Color.Black, rect, BorderSide.None);
                    }
                    if (_Footer3.Length > 0)
                    {
                        e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                        e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                        RectangleF rect = new RectangleF(e.Graph.ClientPageSize.Width - 250, currentY, 250, 120);
                        e.Graph.DrawString(_Footer3, System.Drawing.Color.Black, rect, BorderSide.None);
                    }
                }
                else
                {
                    if (_Footer1.Length > 0)
                    {
                        e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                        e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                        RectangleF rect = new RectangleF(0, currentY, 250, 120);
                        e.Graph.DrawString(_Footer1, System.Drawing.Color.Black, rect, BorderSide.None);
                    }
                    if (_Footer2.Length > 0)
                    {
                        e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                        e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                        RectangleF rect = new RectangleF((e.Graph.ClientPageSize.Width / 2) - 125, currentY, 250, 120);
                        e.Graph.DrawString(_Footer2, System.Drawing.Color.Black, rect, BorderSide.None);
                    }
                    if (_Footer3.Length > 0)
                    {
                        e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                        e.Graph.Font = new Font("Times New Roman", 11, FontStyle.Bold);
                        RectangleF rect = new RectangleF(e.Graph.ClientPageSize.Width - 250, currentY, 250, 120);
                        e.Graph.DrawString(_Footer3, System.Drawing.Color.Black, rect, BorderSide.None);
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public void FormatXtraGridView(DevExpress.XtraGrid.GridControl grdGridFormat)
        {            
            foreach (DevExpress.XtraGrid.Views.BandedGrid.BandedGridView grvGridFormat in grdGridFormat.Views)
            {  
                foreach (DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn col in grvGridFormat.Columns)
                {
                    if (col.UnboundType != DevExpress.Data.UnboundColumnType.Boolean)
                    {
                        col.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
                        DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit repositoryItemMemoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
                        repositoryItemMemoEdit.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                        repositoryItemMemoEdit.Appearance.Options.UseTextOptions = true;
                        grdGridFormat.RepositoryItems.Add(repositoryItemMemoEdit);
                        repositoryItemMemoEdit.DisplayFormat.FormatType = col.DisplayFormat.FormatType;
                        repositoryItemMemoEdit.DisplayFormat.Format = col.DisplayFormat.Format;
                        repositoryItemMemoEdit.DisplayFormat.FormatString = col.DisplayFormat.FormatString;

                        repositoryItemMemoEdit.EditFormat.FormatType = col.DisplayFormat.FormatType;
                        repositoryItemMemoEdit.EditFormat.Format = col.DisplayFormat.Format;
                        repositoryItemMemoEdit.EditFormat.FormatString = col.DisplayFormat.FormatString;
                        col.ColumnEdit = repositoryItemMemoEdit;

                        if (col.ColumnType.Name == "DateTime")
                        {
                            col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            col.DisplayFormat.FormatString = "dd/MM/yyyy";
                            col.RealColumnEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            col.RealColumnEdit.EditFormat.FormatString = "dd/MM/yyyy";
                        }
                    }
                }
            }
        }

        public void DinhDangGridBandVaColumns(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView BGV, bool printHeader)
        {
            DinhDangGridBandVaColumns(BGV);
            BGV.OptionsPrint.PrintHeader = printHeader;
            BGV.OptionsView.ShowColumnHeaders = printHeader;
        }
        public void DinhDangGridBandVaColumns(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView BGV)
        {
            FormatNumberInGridView(BGV);
            BGV.OptionsBehavior.Editable = false;
            BGV.OptionsCustomization.AllowGroup = false;
            BGV.OptionsPrint.AutoWidth = false;
            BGV.OptionsPrint.PrintHeader = false;
            BGV.OptionsPrint.UsePrintStyles = false;            
         
            BGV.OptionsSelection.EnableAppearanceFocusedCell = false;
            BGV.OptionsView.ColumnAutoWidth = false;
            BGV.OptionsView.EnableAppearanceEvenRow = false;
            BGV.OptionsView.EnableAppearanceOddRow = false;
            BGV.OptionsView.RowAutoHeight = true;
            BGV.OptionsView.ShowAutoFilterRow = true;
            BGV.OptionsView.ShowColumnHeaders = false;
            BGV.OptionsView.ShowGroupPanel = false;
            BGV.OptionsView.ShowIndicator = false;

            BGV.Appearance.BandPanel.Options.UseTextOptions = true;
            BGV.Appearance.BandPanel.Font = new Font("Times new Roman", 11F, System.Drawing.FontStyle.Bold);
            BGV.Appearance.BandPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            BGV.Appearance.BandPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            BGV.Appearance.BandPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            BGV.Appearance.BandPanel.TextOptions.Trimming = DevExpress.Utils.Trimming.Word;

            BGV.CustomDrawBandHeader += new DevExpress.XtraGrid.Views.BandedGrid.BandHeaderCustomDrawEventHandler(BGV_CustomDrawBandHeader);
            BGV.CustomDrawColumnHeader += new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler(BGV_CustomDrawColumnHeader);
            BGV.AppearancePrint.BandPanel.BackColor = System.Drawing.Color.White;
            BGV.AppearancePrint.BandPanel.Options.UseBackColor = true;
            BGV.AppearancePrint.BandPanel.BorderColor = System.Drawing.Color.Black;
            BGV.AppearancePrint.BandPanel.Options.UseBorderColor = true;
            BGV.AppearancePrint.BandPanel.Options.UseTextOptions = true;
            BGV.AppearancePrint.BandPanel.Font = new Font("Times new Roman", 11F, System.Drawing.FontStyle.Bold);
            BGV.AppearancePrint.BandPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            BGV.AppearancePrint.BandPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            BGV.AppearancePrint.BandPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            BGV.AppearancePrint.BandPanel.TextOptions.Trimming = DevExpress.Utils.Trimming.Word;
                        
            BGV.Appearance.HeaderPanel.Options.UseTextOptions = true;
            BGV.Appearance.HeaderPanel.Options.UseFont = true;
            BGV.Appearance.HeaderPanel.Font = new Font("Times new Roman", 11F, System.Drawing.FontStyle.Bold);
            BGV.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            BGV.Appearance.HeaderPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            BGV.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            BGV.Appearance.HeaderPanel.TextOptions.Trimming = DevExpress.Utils.Trimming.Word;
           
            BGV.AppearancePrint.HeaderPanel.Options.UseTextOptions = true;
            BGV.AppearancePrint.HeaderPanel.Options.UseFont = true;
            BGV.AppearancePrint.HeaderPanel.Font = new Font("Times new Roman", 11F, System.Drawing.FontStyle.Bold);
            BGV.AppearancePrint.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            BGV.AppearancePrint.HeaderPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            BGV.AppearancePrint.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            BGV.AppearancePrint.HeaderPanel.TextOptions.Trimming = DevExpress.Utils.Trimming.Word;           
                      
            BGV.AppearancePrint.Lines.BackColor = System.Drawing.Color.Black;
            BGV.Appearance.Row.Font = new Font("Times new Roman", 11F, System.Drawing.FontStyle.Regular);
            try
            {
                BGV.Bands[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                BGV.Bands[0].AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
            catch { }
        }

        void BGV_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            e.Column.AppearanceHeader.BackColor = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.BackColor;
            e.Column.AppearanceHeader.BorderColor = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.BorderColor;
            e.Column.AppearanceHeader.Font = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.Font;
            e.Column.AppearanceHeader.TextOptions.HAlignment = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.HAlignment;
            e.Column.AppearanceHeader.TextOptions.VAlignment = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.VAlignment;
            e.Column.AppearanceHeader.TextOptions.WordWrap = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.WordWrap;
            e.Column.AppearanceHeader.TextOptions.Trimming = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.Trimming;            
        }

        void BGV_CustomDrawBandHeader(object sender, DevExpress.XtraGrid.Views.BandedGrid.BandHeaderCustomDrawEventArgs e)
        {
            e.Band.AppearanceHeader.BackColor = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.BackColor;
            e.Band.AppearanceHeader.BorderColor = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.BorderColor;
            e.Band.AppearanceHeader.Font = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.Font;
            e.Band.AppearanceHeader.TextOptions.HAlignment = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.HAlignment;
            e.Band.AppearanceHeader.TextOptions.VAlignment = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.VAlignment;
            e.Band.AppearanceHeader.TextOptions.WordWrap = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.WordWrap;
            e.Band.AppearanceHeader.TextOptions.Trimming = ((DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)sender).AppearancePrint.BandPanel.TextOptions.Trimming;            
        }      
        private void btnBasicExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string _KyBaoCao = null;
        public string KyBaoCao
        {
            get { return _KyBaoCao; }
        }

        private void FrmSupportPrintComponents_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;            
            Lists.Config cf = DB.Configs.FirstOrDefault(p => p.Name == "CompanyName");
            if (cf != null) tenCongTy = cf.Value;
            cf = DB.Configs.FirstOrDefault(p => p.Name == "CompanyAddress");
            if (cf != null) diaChiCongTy = cf.Value;
            dtNgayBatDau.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
            dtNgayKetThuc.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 1, 0, 0, 0).AddSeconds(-1);
        }            

        private void btnBasicLoadReport_Click(object sender, EventArgs e)
        {
            try
            {
                BasicForms.FrmProgressBar.StartShowProgress();
                LoadDataReport();
            }
            catch
            {
            }
            finally
            {
                BasicForms.FrmProgressBar.FinishShowProgress();
            }
        }

        protected virtual void LoadDataReport()
        {
            bandedGridView1.Columns.Clear();
            bandedGridView1.Bands.Clear();
        }

        private void btnBasicPreviewReport_Click(object sender, EventArgs e)
        {
            PreviewReport();
        }

        protected virtual void PreviewReport()
        {
        }

        protected virtual void bandedGridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            try
            {
                string displayText = bandedGridView1.GetRowCellDisplayText(e.RowHandle, "style");
                if (displayText.Equals("bold"))
                {
                    Font font = e.Appearance.Font;
                    e.Appearance.Font = new Font(font.Name, font.Size, FontStyle.Bold);
                }
                else if (displayText.Equals("italic"))
                {
                    Font font = e.Appearance.Font;
                    e.Appearance.Font = new Font(font.Name, font.Size, FontStyle.Italic);
                }
                else if (displayText.Equals("bold_italic"))
                {
                    Font font = e.Appearance.Font;
                    e.Appearance.Font = new Font(font.Name, font.Size, (FontStyle.Bold | FontStyle.Italic));
                }
            }
            catch { }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape) this.Close();
            if (keyData == Keys.F5) btnBasicLoadReport.PerformClick();
            if (keyData == Keys.F7) btnBasicPreviewReport.PerformClick();
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
